package com.wifiunion.zmkm.utils;

public class MakeCode
{
    private final static String HEX = "0123456789ABCDEF";
    private int BIT0 = 0x01;
    private int BIT1 = 0x02;
    private int BIT2 = 0x04;
    private int BIT3 = 0x08;
    private int BIT4 = 0x10;
    private int BIT5 = 0x20;
    private int BIT6 = 0x40;
    private int BIT7 = 0x80;

    private int BIT8 = 0x0100;
    private int BIT9 = 0x0200;
    private int BIT10 = 0x0400;
    private int BIT11 = 0x0800;
    private int BIT12 = 0x1000;
    private int BIT13 = 0x2000;
    private int BIT14 = 0x4000;
    private int BIT15 = 0x8000;

    public int CRC16(int n, int[] p)
    {
        int i, k, j;
        int CRC;
        // 首先清空校验和
        CRC = 0; // Clear CRC
        for (i = 0; i < n; i++)
        {
            // 取需要校验的字节
            j = p[i];
            // 每个字节循环校验8次
            for (k = 0; k < 8; k++)
            {
                // 十六位的值循环右移一位，并把原来的第0位移到第15位
                if ((CRC & BIT0) > 0)
                {
                    CRC >>= 1;
                    CRC |= BIT15;
                }
                else
                {
                    CRC >>= 1;
                    CRC &= 0x7fff; // ~BIT15;
                }
                // 如果需要校验的字节的第0位为1，则取反校验和的第15位
                if ((j & BIT0) > 0)
                {
                    CRC ^= BIT15;
                }
                // 如果校验和的第15位为1，则取反校验和的第10位和第3位
                if ((CRC & BIT15) > 0)
                {
                    CRC ^= BIT10;
                    CRC ^= BIT3;
                }
                // 需要校验的字节循环右移一位
                if ((j & BIT0) > 0)
                {
                    j >>= 1;
                    j |= BIT7;
                }
                else
                {
                    j >>= 1;
                    j &= 0x7f; // ~BIT7;
                }
            }
        }
        // 如果包含校验和校验，则最后得到的校验和值为0，否则出错
        return CRC;
    }

    public String makecard(int nType, String CardData)
    {

        // CardData =
        // "C4|W127|D2|M5628|Y0|Q0000|S1405171129|I1401010000|O1512300000|J1512300000";

        byte[] mpwd1 = new byte[16];
        byte[] mpwd2 = new byte[16];
        byte[] mpwd3 = new byte[16];
        byte[] mpwd4 = new byte[16];
        for (int mm = 8; mm < 16; mm++)
        {
            mpwd1[mm] = (byte) mm;
            mpwd2[mm] = (byte) mm;
            mpwd3[mm] = (byte) mm;
            mpwd4[mm] = (byte) mm;
        }

        int jiaoyan = 3;
        int[] Card_array = new int[64];

        String card_data = "", qrcodestring = "", xingming = "", shenfenzheng = "", beizhu = "", people = "", chenghu = "", xingbie = "";
        String cardType = "", cardno = "", cardbuild = "", cardfloor = "", cardroom = "", cardarea = "", cardauthorization = "", miyao = "", dai = "", coID = "";
        String cardflag = "", cardsubroom = "", cardbgntime = "", powertime = "", cardendtime = "", lostno = "", roomtype = "", baojing = "", wurao = "", shuliang = "", dingdan = "", DLL = "";
        String duandian = "", laiyuan = "";
//        System.err.println(CardData);
        String[] temp = null;

        temp = CardData.split(",");

        for (int i = 0; i < temp.length; i++)
        {

            if (temp[i].substring(0, 1).equals("T"))
            {
                cardType = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("C"))
            {
                cardno = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("L"))
            {
                lostno = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("X"))
            {
                DLL = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("B"))
            {
                cardbuild = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("F"))
            {
                cardfloor = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("R"))
            {
                cardroom = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("A"))
            {
                cardarea = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("M"))
            {
                cardauthorization = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("Y"))
            {
                cardflag = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("S"))
            {
                cardsubroom = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("I"))
            {
                cardbgntime = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("O"))
            {
                cardendtime = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("J"))
            {
                powertime = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("Q"))
            {
                baojing = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("W"))
            {
                wurao = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("N"))
            {
                shuliang = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("D"))
            {
                dingdan = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("P"))
            {
                people = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("Z"))
            {
                duandian = temp[i].substring(1);
            }

            if (temp[i].substring(0, 1).equals("V"))
            {
                laiyuan = temp[i].substring(1);
            }

        }

        if (shuliang.equals(""))
        {
            for (int mm = 1; mm < 8; mm++)
            {
                mpwd1[mm] = (byte) mm;
                // System.err.println(mpwd1[mm]);
            }
            mpwd1[0] = 0x03;

        }
        else
        {
            char[] mima = new char[8];
            mima = shuliang.toCharArray();
            for (int mm = 0; mm < 8; mm++)
            {
                int a = Integer.valueOf(mima[mm]);
                mpwd1[mm] = (byte) a;
            }

        }
        // System.err.println(dingdan);
        if (dingdan.equals(""))
        {
            for (int mm = 1; mm < 8; mm++)
            {
                mpwd2[mm - 1] = (byte) mm;
            }
            mpwd2[7] = 0x03;
        }
        else
        {
            char[] mima2 = new char[8];
            mima2 = dingdan.toCharArray();
            for (int mm = 0; mm < 8; mm++)
            {
                int b = Integer.valueOf(mima2[mm]);
                mpwd2[mm] = (byte) b;
            }
        }

        if (wurao.equals(""))
        {
            for (int mm = 1; mm < 8; mm++)
            {
                mpwd3[mm - 1] = (byte) mm;
            }
            mpwd3[7] = 0x03;
        }
        else
        {
            char[] mima3 = new char[8];
            mima3 = wurao.toCharArray();
            for (int mm = 0; mm < 8; mm++)
            {
                int c = Integer.valueOf(mima3[mm]);
                mpwd3[mm] = (byte) c;
            }
        }

        if (baojing.equals(""))
        {
            for (int mm = 1; mm < 8; mm++)
            {
                mpwd4[mm - 1] = (byte) mm;
            }
            mpwd4[7] = 0x03;
        }
        else
        {
            char[] mima4 = new char[8];
            mima4 = baojing.toCharArray();
            for (int mm = 0; mm < 8; mm++)
            {
                int d = Integer.valueOf(mima4[mm]);
                mpwd4[mm] = (byte) d;
            }
        }

        if (powertime.equals(""))
        {
            powertime = cardbgntime;
        }

        if (people.equals(""))
        {
            people = "1";
        }

        switch (nType)
        {
            case 15:
            {
                if (DLL.equals(""))
                {
                    DLL = "24";
                }
                else
                {
                    DLL = "56";
                }
                break;
            }
            case 0:
            {
                if (DLL.equals(""))
                {
                    DLL = "24";
                }
                else
                {
                    DLL = "56";
                }

                break;
            }
            case 8:
            {
                if (DLL.equals(""))
                {
                    DLL = "24";
                }

                break;
            }
            case 3:
            {
                if (DLL.equals(""))
                {
                    DLL = "24";
                }
                else
                {
                    DLL = "56";
                }
                break;
            }
            case 1:
            {

                if (DLL.equals(""))
                {
                    DLL = "1";
                }

                break;
            }
            case 2:
            {
                if (DLL.equals(""))
                {
                    DLL = "24";
                }

                break;
            }
            case 10:
            {
                if (DLL.equals(""))
                {
                    DLL = "3";
                }

                break;
            }
            case 14:
            {

                if (DLL.equals(""))
                {
                    DLL = "24";
                }

                break;
            }
            case 4:
            {
                if (DLL.equals(""))
                {
                    DLL = "24";
                }
                else
                {
                    DLL = "56";
                }
                break;
            }
            case 5:
            {
                if (DLL.equals(""))
                {
                    DLL = "24";
                }
                else
                {
                    DLL = "56";
                }
                break;
            }
            case 6:
            {
                if (DLL.equals(""))
                {
                    DLL = "24";
                }// 56
                else
                {
                    DLL = "56";
                }
                break;
            }
            case 7:
            {
                if (DLL.equals(""))
                {
                    DLL = "56";
                }

                break;
            }
            case 11:
            {
                if (DLL.equals(""))
                {
                    DLL = "24";
                }

                break;
            }
            case 13:
            {
                if (DLL.equals(""))
                {
                    DLL = "34";
                }

                break;
            }

            default:
                ;
        }

        if (cardflag.equals(""))
        {
            cardflag = "0";
        }

        if (cardauthorization.equals(""))
        {
            cardauthorization = "0";
        }

        cardType = Integer.toString(nType);

        for (int i = 0; i < 40; i++)
        {
            Card_array[i] = 0x00;
        }
        int m = 0;

        Card_array[m++] = 0xDE;
        Card_array[m++] = Integer.valueOf(people);
        Card_array[m++] = (Integer.valueOf(cardauthorization) / 256);
        Card_array[m++] = (Integer.valueOf(cardauthorization) % 256);

        Card_array[m++] = Integer.valueOf(DLL);

        switch (nType)
        {
            case 15:
            {
                Card_array[m++] = Integer.valueOf(cardroom); // 房号 //m:=5;
                Card_array[m++] = Integer.valueOf(cardfloor); // 楼层
                Card_array[m++] = Integer.valueOf(cardbuild); // 楼栋
                break;
            }
            case 0:
            {
                Card_array[m++] = Integer.valueOf(cardroom); // 房号 //m:=5;
                Card_array[m++] = Integer.valueOf(cardfloor); // 楼层
                Card_array[m++] = Integer.valueOf(cardbuild); // 楼栋
                break;
            }
            case 8:
            {
                Card_array[m++] = Integer.valueOf(cardroom); // 房号 //m:=5;
                Card_array[m++] = Integer.valueOf(cardfloor); // 楼层
                Card_array[m++] = Integer.valueOf(cardbuild); // 楼栋
                break;
            }
            case 3:
            {
                Card_array[m++] = 0;
                Card_array[m++] = Integer.valueOf(cardarea);
                Card_array[m++] = 0;// Random(255) ;
                break;
            }
            case 1:
            {

                Card_array[m++] = 0;// Random(255);
                Card_array[m++] = Integer.valueOf(cardarea);
                Card_array[m++] = Integer.valueOf(wurao);
                break;
            }
            case 2:
            {

                Card_array[m++] = 0;// Random(255) ;
                Card_array[m++] = 0;// Random(255) ;
                Card_array[m++] = 0;// Random(255) ;
                break;
            }
            case 10:
            {
                Card_array[m++] = 0;// Random(255);
                Card_array[m++] = 0;// Random(255) ;
                Card_array[m++] = 0;// Random(255) ;
                break;
            }
            case 14:
            {

                Card_array[m++] = 0;// Random(255);
                Card_array[m++] = 0;// Random(255) ;
                Card_array[m++] = 0;// Random(255) ;
                break;
            }
            case 4:
            {
                Card_array[m++] = 0;
                Card_array[m++] = Integer.valueOf(cardfloor);
                Card_array[m++] = Integer.valueOf(cardbuild);
                break;
            }
            case 5:
            {
                Card_array[m++] = 0;
                Card_array[m++] = 0;// Random(255) ;
                Card_array[m++] = Integer.valueOf(cardbuild);
                break;
            }
            case 6:
            {
                Card_array[m++] = 0;
                Card_array[m++] = 0x5A;// Random(255) ;
                Card_array[m++] = 0x95;// Random(255) ;
                break;
            }
            case 7:
            {
                Card_array[m++] = 0;
                Card_array[m++] = 0;// Random(255) ;
                Card_array[m++] = 0;// Random(255) ;
                break;
            }
            case 11:
            {
                Card_array[m++] = 0;// Random(255) ;
                Card_array[m++] = 0;// Random(255) ;
                Card_array[m++] = 0;// Random(255) ;
                break;
            }
            case 13:
            {
                Card_array[m++] = (Integer.valueOf(cardauthorization) / 256) & 0xff;
                Card_array[m++] = (Integer.valueOf(cardauthorization) % 256) & 0xff;
                Card_array[m++] = 128;
                break;
            }

            default:
                ;
        }
        // m=7;

        switch (nType)
        {
            case 0:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (6 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 15:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (9 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 1:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (5 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 2:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (7 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }

            case 3:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (8 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 4:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (13 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 5:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (12 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 6:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (11 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 7:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (10 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 8:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (2 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 10:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (1 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }

            case 11:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (4 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 13:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            case 14:
            {
                Card_array[m++] = Integer.valueOf(cardflag);
                Card_array[m++] = (3 * 16 + Integer.valueOf(cardno)); // 卡型+ 卡号
                break;
            }
            default:
                ;
        }

        Card_array[m++] = Integer.valueOf(cardsubroom.substring(2, 4)); // 发卡时间
        Card_array[m++] = (Integer.valueOf(cardsubroom.substring(4, 6)) * 8 + (Integer
                .valueOf(cardsubroom.substring(6, 8)) / 4));
        Card_array[m++] = ((Integer.valueOf(cardsubroom.substring(6, 8)) % 4) * 64 + Integer
                .valueOf(cardsubroom.substring(8, 10)));

        Card_array[m++] = 4;
        Card_array[m++] = Integer.valueOf(cardsubroom.substring(0, 2)) - 14;

        switch (nType)
        {

            case 0:
            {
                // powertime
                Card_array[m++] = 16
                        * (Integer.valueOf(cardbgntime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardbgntime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardbgntime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardbgntime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardbgntime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardbgntime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(cardendtime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardendtime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardendtime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardendtime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardendtime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardendtime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(powertime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(powertime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(powertime.substring(4, 6)) * 8 + (Integer
                        .valueOf(powertime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer.valueOf(powertime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(powertime.substring(8, 10)));

                break;
            }
            case 15:
            {
                Card_array[m++] = 16
                        * (Integer.valueOf(cardbgntime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardbgntime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardbgntime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardbgntime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardbgntime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardbgntime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(cardendtime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardendtime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardendtime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardendtime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardendtime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardendtime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(powertime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(powertime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(powertime.substring(4, 6)) * 8 + (Integer
                        .valueOf(powertime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer.valueOf(powertime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(powertime.substring(8, 10)));

                break;
            }
            case 1:
            {

                break;
            }
            case 2:
            {

                break;
            }

            case 3:
            {
                Card_array[m++] = 16
                        * (Integer.valueOf(cardbgntime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardbgntime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardbgntime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardbgntime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardbgntime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardbgntime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(cardendtime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardendtime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardendtime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardendtime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardendtime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardendtime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(powertime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(powertime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(powertime.substring(4, 6)) * 8 + (Integer
                        .valueOf(powertime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer.valueOf(powertime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(powertime.substring(8, 10)));

                if (baojing.equals(""))
                {
                    baojing = cardbgntime.substring(6, 10);
                }

                if ((nType >= 3) && (nType <= 7))
                {
                    Card_array[m++] = 8
                            * Integer.valueOf(baojing.substring(0, 2), 16)
                            + Integer.valueOf(baojing.substring(2, 4), 16);
                    ;
                }
                break;
            }
            case 4:
            {
                Card_array[m++] = 16
                        * (Integer.valueOf(cardbgntime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardbgntime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardbgntime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardbgntime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardbgntime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardbgntime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(cardendtime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardendtime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardendtime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardendtime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardendtime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardendtime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(powertime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(powertime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(powertime.substring(4, 6)) * 8 + (Integer
                        .valueOf(powertime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer.valueOf(powertime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(powertime.substring(8, 10)));

                if (baojing.equals(""))
                {
                    baojing = cardbgntime.substring(6, 10);
                }

                if ((nType >= 3) && (nType <= 7))
                {
                    Card_array[m++] = 8
                            * Integer.valueOf(baojing.substring(0, 2), 16)
                            + Integer.valueOf(baojing.substring(2, 4), 16);
                    ;
                }
                break;
            }
            case 5:
            {
                Card_array[m++] = 16
                        * (Integer.valueOf(cardbgntime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardbgntime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardbgntime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardbgntime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardbgntime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardbgntime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(cardendtime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardendtime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardendtime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardendtime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardendtime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardendtime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(powertime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(powertime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(powertime.substring(4, 6)) * 8 + (Integer
                        .valueOf(powertime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer.valueOf(powertime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(powertime.substring(8, 10)));

                if (baojing.equals(""))
                {
                    baojing = cardbgntime.substring(6, 10);
                }

                if ((nType >= 3) && (nType <= 7))
                {
                    Card_array[m++] = 8
                            * Integer.valueOf(baojing.substring(0, 2), 16)
                            + Integer.valueOf(baojing.substring(2, 4), 16);
                    ;
                }
                break;
            }
            case 6:
            {
                Card_array[m++] = 16
                        * (Integer.valueOf(cardbgntime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardbgntime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardbgntime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardbgntime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardbgntime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardbgntime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(cardendtime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardendtime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardendtime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardendtime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardendtime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardendtime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(powertime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(powertime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(powertime.substring(4, 6)) * 8 + (Integer
                        .valueOf(powertime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer.valueOf(powertime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(powertime.substring(8, 10)));

                if (baojing.equals(""))
                {
                    baojing = cardbgntime.substring(6, 10);
                }

                if ((nType >= 3) && (nType <= 7))
                {
                    Card_array[m++] = 8
                            * Integer.valueOf(baojing.substring(0, 2), 16)
                            + Integer.valueOf(baojing.substring(2, 4), 16);
                    ;
                }
                break;
            }
            case 7:
            {
                Card_array[m++] = 16
                        * (Integer.valueOf(cardbgntime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardbgntime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardbgntime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardbgntime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardbgntime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardbgntime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(cardendtime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(cardendtime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(cardendtime.substring(4, 6)) * 8 + (Integer
                        .valueOf(cardendtime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer
                        .valueOf(cardendtime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(cardendtime.substring(8, 10)));

                Card_array[m++] = 16
                        * (Integer.valueOf(powertime.substring(0, 2)) - Integer
                                .valueOf(cardsubroom.substring(0, 2)))
                        + Integer.valueOf(powertime.substring(2, 4));
                Card_array[m++] = (Integer.valueOf(powertime.substring(4, 6)) * 8 + (Integer
                        .valueOf(powertime.substring(6, 8)) / 4));
                Card_array[m++] = ((Integer.valueOf(powertime.substring(6, 8)) % 4) * 64 + Integer
                        .valueOf(powertime.substring(8, 10)));

                if (baojing.equals(""))
                {
                    baojing = cardbgntime.substring(6, 10);
                }

                if ((nType >= 3) && (nType <= 7))
                {
                    Card_array[m++] = 8
                            * Integer.valueOf(baojing.substring(0, 2), 16)
                            + Integer.valueOf(baojing.substring(2, 4), 16);
                    ;
                }
                break;
            }

            case 13:
            {

                for (int ll = 0; ll < 8; ll++)
                {
                    Card_array[m++] = mpwd3[ll];
                }
                for (int ll = 0; ll < 8; ll++)
                {
                    Card_array[m++] = mpwd4[ll];
                }

                break;
            }

            case 11:
            {
                Card_array[m++] = Integer.valueOf(lostno.substring(0, 2), 16);
                Card_array[m++] = Integer.valueOf(lostno.substring(2, 4), 16);
                Card_array[m++] = Integer.valueOf(lostno.substring(4, 6), 16);
                Card_array[m++] = Integer.valueOf(lostno.substring(6, 8), 16);

                break;
            }

            default:
                ;
        }

        if ((nType > 0) && (nType < 11))
        {
            if (duandian.equals(""))
            {
                duandian = "30";
            }
            Card_array[24] = Integer.valueOf(duandian);
        }

        m = 30;
        int[] data_array = new int[31];
        for (int kk = 0; kk <= m; kk++)
        {
            data_array[kk] = Card_array[kk];
        }
        data_array[0] = 3;

        Card_array[++m] = CRC16(31, data_array) % 256;
        Card_array[++m] = CRC16(31, data_array) / 256;

        // aaa = SimpleCrypto.toHex(ba).toString();
//        System.err.println("-----------------------------------------------");
        String cardstringdata = "";

        for (int i = 0; i < 32; i++)
        {
            cardstringdata = cardstringdata
                    + HEX.charAt(((byte) Card_array[i] >> 4) & 0x0f)
                    + HEX.charAt((byte) Card_array[i] & 0x0f);// Integer.valueOf(Card_array[i])
                                                              // ;
        }

//        System.err.println(cardstringdata);
//        System.err.println("-----------------------------------------------");
        byte[] ab = new byte[33];
        for (int i = 0; i < 16; i++)
        {
            ab[i] = (byte) Card_array[i + 1];
        }

        byte[] encryptdata = new byte[32];

        for (int i = 0; i < 16; i++)
        {
            encryptdata[i] = (byte) Integer.parseInt(SimpleCrypto.toHex(ab)
                    .substring(2 * i, 2 * (i + 1)), 16);
        }

        byte[] result_data = new byte[16];
        byte[] result_data2 = new byte[16];

        try
        {
            result_data = SimpleCrypto.encrypt_byte(mpwd2,
                    SimpleCrypto.encrypt_byte(mpwd1, encryptdata));
            // result_data = SimpleCrypto.encrypt_byte(mpwd2,
            // SimpleCrypto.encrypt_byte(mpwd1, encryptdata));
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SimpleCrypto.toHex(encryptdata);

        byte[] ba = new byte[16];
        for (int i = 0; i < 16; i++)
        {
            ba[i] = (byte) Card_array[i + 17];
        }

        String aaa = "";
        aaa = SimpleCrypto.toHex(ba).toString();

        encryptdata = SimpleCrypto.toByte(aaa);

        char[] aaaabbbb = new char[16];
        char[] bbbbaaaa = new char[16];

        for (int b = 0; b < 16; b++)
        {
            aaaabbbb[b] = (char) Integer.parseInt(SimpleCrypto.toHex(ba)
                    .substring(2 * b, 2 * (b + 1)), 16);
        }

        encryptdata = SimpleCrypto.getBytes(aaaabbbb);

        try
        {
            result_data2 = SimpleCrypto.encrypt_byte(mpwd2,
                    SimpleCrypto.encrypt_byte(mpwd1, encryptdata));
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }

        char aabb = 0xDE;
        char bbaa;
        if (nType == 13)
        {
            bbaa = 0xAC;
        }
        else
        {
            bbaa = 0xE1;
        }
        char[] aaabbb = new char[16];
        char[] bbbaaa = new char[16];

        String jieguo = "", jieguo2 = "";
        for (int b = 0; b < 16; b++)
        {
            jieguo = jieguo
                    + Integer.parseInt(SimpleCrypto.toHex(result_data)
                            .substring(2 * b, 2 * (b + 1)), 16);
            jieguo2 = jieguo2
                    + Integer.parseInt(SimpleCrypto.toHex(result_data2)
                            .substring(2 * b, 2 * (b + 1)), 16);
            aaabbb[b] = (char) Integer.parseInt(SimpleCrypto.toHex(result_data)
                    .substring(2 * b, 2 * (b + 1)), 16);
            bbbaaa[b] = (char) Integer.parseInt(SimpleCrypto
                    .toHex(result_data2).substring(2 * b, 2 * (b + 1)), 16);
        }

        System.err.println(jieguo + jieguo2);
        String xxx = "";

        for (int i = 0; i < 16; i++)
        {
            xxx = xxx + aaabbb[i];

        }
        for (int i = 0; i < 16; i++)
        {
            xxx = xxx + bbbaaa[i];

        }

        return aabb + xxx + bbaa;

    }

}
